package bibbletex.helpers;

import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * SQL Helper class for all the database transactions
 * 
 * @author Diego Barrios Romero
 *
 */
public class SQLHelper {

	/**
	 * Method to execute an update sql statement (no return)
	 *  
	 * @param sql SQL statement
	 * @return Whether the execution was successful
	 */
	public static boolean execSQL(String sql) {
		boolean success = false;
		try {
			java.sql.Statement statement = DBConnector.getConnection()
					.createStatement();

			statement.setQueryTimeout(5); // 30s

			statement.executeUpdate(sql);
			success = true;
		} catch (SQLException e) {
			System.err.println(e);
		}

		return success;
	}

	/**
	 * Method to execute a sql query and return the results
	 *  
	 * @param sql SQL statement
	 * @return result of the query
	 */
	public static ResultSet execQuerySQL(String sql) {
		ResultSet rs = null;
		try {
			java.sql.Statement statement = DBConnector.getConnection()
					.createStatement();

			statement.setQueryTimeout(5); // 30s

			rs = statement.executeQuery(sql);
		} catch (SQLException e) {
			System.err.println(e.getMessage());
		}

		return rs;
	}
}
